package com.eight.controller.api;

import com.eight.feign.TbUserFeign;
import com.eight.pojo.TbOrder;
import com.eight.service.OrderService;
import com.eight.util.JWTTool;
import com.eight.util.WeChatPayUtil;
import com.eight.vo.ComResult;
import com.eight.vo.InOrderPayVo;
import com.eight.vo.WeChatComData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/system/api/order")
public class OrderApi {
/*注入订单service*/
    @Autowired private OrderService orderService;
    /*注入userFeign皆苦*/
    @Autowired private TbUserFeign tbUserFeign;

    @Autowired private StringRedisTemplate stringRedisTemplate;

    /**
     * 根据用户名称查询订单
     * @param name 用户名
     * @return 返回订单信息条数
     */
    @RequestMapping("selectByName")
    public ComResult selectByName(String name){
        return new ComResult(2000,"查询用户的订单",orderService.selectByName(name));
    }

    /**
     * 根据订单状态查询订单
     * @param status 订单的状态
     * @return 返回符合条件的订单
     */
    @RequestMapping("selectOrderByStatus/{status}")
    public ComResult selectOrderByNameAndStatus(@PathVariable Integer status){
        return new ComResult(2000,"查询/已支付/未支付/已失效订单",orderService.selectOrderByStatus(status));
    }

    /**
     * 去掉订单
     * @param tbOrder 订单号以及订单状态u
     * @return 返回修改结果
     */
    @PostMapping("updateOrderStart")
    public ComResult updateOrderStart(@RequestBody TbOrder tbOrder){
        return new ComResult(2000,"取消订单成功",orderService.updateStateByOrderNo(tbOrder));
    }

    /**
     * vx支付生成二维码
     * @param tbOrder 根据订单号 以及支付的金额
     * @return 返回二维码连接
     */
    @PostMapping("VXPay")
    public ComResult VXPay(@RequestBody TbOrder tbOrder){
        WeChatComData weChatComData = new WeChatComData();
        weChatComData.setSerialNumber(tbOrder.getOrderNo());
        weChatComData.setTitle(tbOrder.getOrderNo());
        // 需要支付的钱
        weChatComData.setAmount(tbOrder.getPreparidAmount());
        WeChatComData weChatComData1 = WeChatPayUtil.makePayOrder(weChatComData);
        return new  ComResult( 2000,"生成二维码",weChatComData1);
    }

    /**
     * 是否支付校验 支付过了 就修改订单状态
     * @param tbOrder 订单号
     * @return 返回修改结果
     */
    @PostMapping("/isOrder")
    public ComResult isOrder(@RequestBody TbOrder tbOrder){
        boolean b = WeChatPayUtil.queryPayStatus(tbOrder.getOrderNo());
        if (b){
            // 支付成功将订单号存入redis中 进行结束延迟列队
            stringRedisTemplate.opsForValue().set(tbOrder.getOrderNo(),"",30, TimeUnit.MINUTES);
            tbOrder.setState(1);
            return new ComResult(2000,"已支付",orderService.updateStateByOrderNo(tbOrder));
        }
        return new ComResult(5000,"尚未支付");
    }

    /**
     * 余额支付 如果支付成功就修改订单状态 没有就不修改
     * @param payVo 用户名称 需支付的金额 以及订单号
     * @return 返回修改结果
     */
    @PostMapping("/inOrderPay")
    public ComResult inOrderPay(@RequestBody InOrderPayVo payVo){
        if (tbUserFeign.updateUserMoney(payVo.getUsername(), payVo.getTotalPrice())>0){
            stringRedisTemplate.opsForValue().set(payVo.getOrderNo(),"",30, TimeUnit.MINUTES);
            return new ComResult(2000,"支付成功",orderService.inOrderPayByOrderNo(payVo));
        }
        return new ComResult(5000,"支付失败");

    }

    /**
     *
     * @param carId
     * @return
     */
    @GetMapping("/getCarComment/{carId}")
    public ComResult getCarComment(@PathVariable int carId){
        return new ComResult(2000,"根据车辆id查询该车辆的评价",orderService.getCarComment(carId));
    }




}
